package com.cby.operation.login.ui

import android.app.Fragment
import android.content.ComponentName
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import com.cby.operation.common.encrypt.Base64Utils
import com.cby.operation.common.encrypt.Md5Utils
import com.cby.operation.common.util.AppUtils
import com.cby.operation.common.util.CbyLogUtils
import com.cby.operation.initialize.InitManager
import com.cby.operation.initialize.data.InitConfig
import com.cby.operation.login.LoginManager
import com.cby.operation.sdk.*

/**
 * Description:登录调用或者账号切换
 *
 * @author 陈宝阳
 * @create 2020/8/3 18:28
 */
class LoginAuthFragment : Fragment() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        userInfoFromHykbApp
    }

    // md5(“SFlLQitMT0dJTg”+apk签名) Md5Utils.md5("SFlLQitMT0dJTg" + AppUtils.getSignInfo(this))
    val userInfoFromHykbApp: Unit
        //调用快爆登陆验证的固定标识
        get() {
            val initConfig: InitConfig? = InitManager.instance.initConfig
            val appId: String? = SdkConfiguration.gameId
            if (initConfig == null || TextUtils.isEmpty(appId)) {
                CbyLogUtils.e(TAG, "SDK未进行初始化或者SDK初始化配置信息获取失败！")
                return
            }
            val intent = Intent()
            val componentName = ComponentName(
                HYKB_PACKAGE_NAME,
                HYKB_LOGIN_ACTIVITY
            )
            intent.component = componentName
            intent.putExtra(KEY_APP_ID, appId)
            intent.putExtra(KEY_GAME_NAME, initConfig.gameName)
            intent.putExtra(KEY_GAME_ICON, initConfig.gameIcon)
            // md5(“SFlLQitMT0dJTg”+apk签名) Md5Utils.md5("SFlLQitMT0dJTg" + AppUtils.getSignInfo(this))
            val sha1: String? = AppUtils.getSignInfo(activity)
            CbyLogUtils.flog(sha1)
            val sign: String = Md5Utils.md5(Base64Utils.decode(SIGN_PREV_SALTS).toString() + sha1)
            intent.putExtra(KEY_SIGN_INFO, sign)
            //调用快爆登陆验证的固定标识
            intent.putExtra(
                KEY_USER_INFO_SDK_QUEST,
                VALUE_USER_INFO_SDK_QUEST
            )
            startActivityForResult(intent, REQUEST_CODE_USER)
        }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_USER) {
            if (resultCode == CODE_LOGIN_SUCCESS && data != null) {
                //用户授权校验成功后的数据返回
                val uid = data.getStringExtra(KEY_USER_ID)
                val token = data.getStringExtra(KEY_USER_TOKEN)
                val type = data.getStringExtra(KEY_USER_LOGIN_TYPE)
                val nick = data.getStringExtra(KEY_USER_NICK)
                if (TextUtils.isEmpty(uid)
                    || TextUtils.isEmpty(type)
                    || TextUtils.isEmpty(token)
                    || TextUtils.isEmpty(nick)
                ) {
                    //数据获取异常
                    LoginManager.instance.handleFailedCallback(
                        CODE_LOGIN_DATA_EXCEPTION,
                        MSG_LOGIN_DATA_EXCEPTION
                    )
                } else {
                    LoginManager.instance.createInternalUser(
                        uid.toString(),
                        nick.toString(),
                        type.toString(),
                        token.toString()
                    )
                }
            } else if (resultCode == CODE_LOGIN_FAIL && data != null) {
                //获取用户信息失败的返回
                val msg = data.getStringExtra(KEY_USER_FAILING_MSG)
                LoginManager.instance.handleFailedCallback(resultCode, msg)
            } else {
                //这边取消有两个情况：一是有登录的情况，二是没有登录的情况下
                LoginManager.instance.handleFailedCallback(CODE_LOGIN_CANCEL, MSG_LOGIN_CANCEL)
            }
        }
    }

    companion object {

        private const val TAG = "login"

        /**
         * 用户登陆校验的请求码
         */
        private const val REQUEST_CODE_USER = 1001

        /**
         * 签名固定前缀，明文：SFlLQitMT0dJTg, base64后  U0ZsTFFpdE1UMGRKVGcK
         */
        private const val SIGN_PREV_SALTS = "U0ZsTFFpdE1UMGRKVGcK"
    }
}